package com.project.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.PurchaseBean;
import com.project.bean.PurchaseProductBean;
import com.project.mapper.IProductMapper;
import com.project.mapper.IPurChaseProductMapper;
import com.project.mapper.IPurchaseMapper;
import com.project.service.IPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class IPurchaseServiceImpl implements IPurchaseService {

    @Autowired
    private IPurchaseMapper purchaseMapper;
    @Autowired
    private IPurChaseProductMapper purChaseProductMapper;
    @Autowired
    private IProductMapper productMapper;

    @Override
    public void addPurchase(PurchaseBean purchase, List<PurchaseProductBean> infoList) {
       purchaseMapper.addPurchase(purchase);
       purChaseProductMapper.addInfo(purchase.getId(),infoList);
       for (PurchaseProductBean info : infoList){
           productMapper.buyProduct(info.getProductBean().getId(),info.getPurchaseNum());
       }
    }

    @Override
    public PageInfo<PurchaseBean> findByItem(int page, int pageSize, String supplier, LocalDate startDate, LocalDate endDate) {
        PageHelper.startPage(page,pageSize);
        PageInfo<PurchaseBean>pageInfo=PageInfo.of(purchaseMapper.findByItem(supplier, startDate, endDate));
        return pageInfo;
    }

    @Override
    public PurchaseBean findById(int id) {
        return purchaseMapper.findById(id);
    }
}
